package com.tql.mall.search.controller;

import com.tql.mall.model.search.Goods;
import com.tql.mall.model.search.SearchParam;
import com.tql.mall.model.search.SearchRespVo;
import com.tql.mall.search.service.SearchService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: tyk
 * @Date: 2023/5/23 18:10
 * @Description: 搜索模块向外暴露的接口
 */
@RestController
@RequestMapping("api/search/inner")
@Api(description = "service-search对内暴露接口")
public class SearchApiController {

    @Autowired
    private ElasticsearchRestTemplate restTemplate;

    @Autowired
    private SearchService searchService;

    /**
     * 创建索引库
     */
    @GetMapping("createIndex")
    @ApiOperation("创建索引库")
    public Boolean createIndex() {
        // 创建索引库
        restTemplate.createIndex(Goods.class);
        // 建立Mapping
        restTemplate.putMapping(Goods.class);
        return true;
    }


    /**
     * 商品上架
     */
    @GetMapping("onSale/{skuId}")
    @ApiOperation("商品上架")
    public Boolean onSale(@PathVariable Long skuId) {
        return searchService.onSale(skuId);
    }

    /**
     * 商品下架
     */
    @GetMapping("offSale/{skuId}")
    @ApiOperation("商品下架")
    public Boolean offSale(@PathVariable Long skuId) {
        return searchService.offSale(skuId);
    }

    /**
     * 增加热度
     */
    @GetMapping("incrHotScore/{skuId}")
    @ApiOperation("增加热度")
    public Boolean incrHotScore(@PathVariable Long skuId) {
        return searchService.incrHotScore(skuId);
    }


    /**
     * 搜索
     */
    @PostMapping("search")
    @ApiOperation("搜索")
    public SearchRespVo search(@RequestBody SearchParam searchParam) {
        return searchService.search(searchParam);
    }


}
